/**
* Copyright @ 2023 iAuto (Shanghai) Co., Ltd.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are NOT permitted except as agreed by
* iAuto (Shanghai) Co., Ltd.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/


import {
  Avatar,
  Button,
  Col,
  Image,
  message,
  Row,
  Descriptions,
  Modal,
  Select,
  DatePicker,
  Form,
} from 'antd';


import {useRequest} from 'ahooks';
import styles from './release.less';
import React, {useState} from 'react';
import IAutoDetail from '../../../../components/soa/IAutoDetail';
import {getScenesDetail, release} from '@/services/soa/sceneOfficial';
import {getDateTime} from "@/utils/soa/utils";
import {router} from "umi";
import moment from "moment";

const Index = (props: any) => {

  const [form] = Form.useForm();
  const {query} = props.location;
  // 请求回来的参数
  const {data, loading: requestLoading} = useRequest(getScenesDetail, {
    defaultParams: [query?.id],
  });

  const releaseRes = useRequest(release, {
    manual: true,
    onSuccess(response) {
      if (response?.statusCode == '0') {
        message.success('发布成功,已提交审核');
        // history.go(-1);
        router.push(
          // `/soaManage/scene/releaseHistory?id=${param?.sceneUuid}&name=${param?.cnName}`,
          `/soaManage/scene/official/releaseHistory?id=${query?.id}`,
        );
      }
    },
  });

  const [modalVisible, setModalVisible] = useState(false); // 控制模态框显隐
  const [releaseType, setReleaseType] = useState(1); // 初始为立即发布

  const options = [
    {label: '立即发布', value: 1},
    {label: '定时发布', value: 0},
  ];

  const handleOk = (values) => {
    const {releaseType} = values;
    const {cronReleaseTime} = values;
    releaseRes.run({
      sceneUuid: query?.id,
      releaseType,
      cronReleaseTime,
    });

    form.resetFields();
    setReleaseType(1);
    setModalVisible(false);
  };

  const handleCancel = () => {
    form.resetFields();
    setReleaseType(1);
    setModalVisible(false);
  };

  const pushHandle = () => {
    setModalVisible(true);
  };

  return (
    <IAutoDetail className={styles.main} title={'场景发布'} loading={requestLoading}>
      <Row justify={'end'}>
        <Col>
          <Button type="primary" onClick={pushHandle}>
            发布
          </Button>
          {modalVisible && (
            <Modal
              title="发布设置"
              visible={modalVisible}
              onOk={() => form.submit()}
              onCancel={handleCancel}
              onClose={handleCancel}
              okText="发布"
            >
              <Form
                form={form}
                onFinish={handleOk}
                initialValues={{releaseType: 1}}>
                <Form.Item
                  name="releaseType"
                  label="发布类型"
                >
                  <Select onChange={(value) => {
                    setReleaseType(value);
                  }}>
                    {options.map((option) => (
                      <Select.Option key={option.value} value={option.value}>
                        {option.label}
                      </Select.Option>
                    ))}
                  </Select>
                </Form.Item>
                {releaseType === 0 && (
                  <Form.Item
                    name="cronReleaseTime"
                    label="期望发布时间"
                    rules={[
                      {
                        required: true,
                        message: '请选择期望发布时间'
                      }
                    ]}
                  >
                    <DatePicker
                      showTime
                      format="YYYY-MM-DD HH:mm"
                      placeholder="请选择时间"
                      showNow={false}
                      disabledDate={(current) => {
                        return current.isBefore(moment(), 'minute');
                      }}
                    />
                  </Form.Item>
                )}
              </Form>
            </Modal>
          )}
        </Col>
      </Row>
      <Descriptions column={1} contentStyle={{whiteSpace: 'normal'}}>
        <Descriptions.Item label={'场景名'}>{data?.data?.name}</Descriptions.Item>
        <Descriptions.Item label={'场景简介'}
                           className={styles.descriptionScene}>{data?.data?.description}</Descriptions.Item>
        <Descriptions.Item label={'品牌'}>
          {data?.data?.brandName}
        </Descriptions.Item>
        <Descriptions.Item label={'车系'}>
          {data?.data?.seriesName}
        </Descriptions.Item>
        <Descriptions.Item label={'车型'}>
          {data?.data?.modelName}
        </Descriptions.Item>
        <Descriptions.Item label={'场景分类'}>{data?.data?.categoryName}</Descriptions.Item>
        <Descriptions.Item label={'是否官方场景'}>{data?.data?.officialScene == 0 ? "否" : "是"}</Descriptions.Item>
        <Descriptions.Item label={'手机端可执行'}>{data?.data?.executedOnPhone == 0 ? "否" : "是"}</Descriptions.Item>
        <Descriptions.Item label={'车机端二次确认'}
                           className={data?.data?.haveStartTrigger === 1 ? styles.showInput : styles.hideInput}>
          {data?.data?.doubleCheckOnVehicle == 0 ? "否" : "是"}
        </Descriptions.Item>
        <Descriptions.Item className={data?.data?.haveStartTrigger === 1 ? styles.showInput : styles.hideInput}
                           label={'周期内执行次数'}>{data?.data?.cycleExecution == 1 ? "单次" : "无限制"}</Descriptions.Item>
        <Descriptions.Item label={'场景图标'}>
          <Avatar shape="square" size={60} src={data?.data?.iconUrl}></Avatar>
        </Descriptions.Item>
        <Descriptions.Item label={'场景背景'}>
          {data?.data?.backgroundPicUrl && (
            <Image
              style={{objectFit: 'contain'}}
              height={156}
              width={286}
              src={data?.data?.backgroundPicUrl}
            />
          )}
        </Descriptions.Item>
        <Descriptions.Item label={'更新时间'}>{getDateTime(data?.data?.updateTime)}</Descriptions.Item>
      </Descriptions>
    </IAutoDetail>
  );
};
export default Index;
